<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * ============================================================================
 * $Author: IT宇宙人 2015-08-10 $
 */ 
namespace Home\Controller;
use Think\Page;
use Think\Verify;
class IndexController extends BaseController {
    
    public function index(){

        // 如果是手机跳转到 手机模块
       if(true == isMobile()){
            header("Location: ".U('Mobile/Index/index'));
        }

        //首页店铺类型图标 及购买流程引到图
        $config = tpCache('index_img');
		//print_r($config);
//       dump($config);
        //查询成功案例
        $where['status']=1;
        $case_list=M('case')->where($where)->order('id DESC')->limit(8)->select();


        foreach($case_list AS $k=>$v){
            if($v['images']){
                $v['goods']=M('goods')->where(array('goods_id'=>$v['goods_id']))->find();
                $case_list[$k]=$v;
            }else{
                unset($case_list[$k]);
            }

        }
        $case_list = array_values(array_filter($case_list));

        if($case_list[0]){$lists1[] = $case_list[0];}
        if($case_list[1]){$lists1[] = $case_list[1];}
        if($case_list[2]){$lists1[] = $case_list[2];}
        if($case_list[3]){$lists1[] = $case_list[3];}
        if($case_list[4]){$lists2[] = $case_list[4];}
        if($case_list[5]){$lists2[] = $case_list[5];}
        if($case_list[6]){$lists2[] = $case_list[6];}
        if($case_list[7]){$lists2[] = $case_list[7];}

        foreach($lists1 AS $key=>$value)
        {
            if(!$value["images"])
            {
                $value["images"] = __ROOT__."/Template/pc/soubao/Static/images/blank.gif";
            }
            $lists1[$key] = $value;
        }
        foreach($lists2 AS $key=>$value)
        {
            if(!$value["images"])
            {
                $value["images"] = __ROOT__."/Template/pc/soubao/Static/images/blank.gif";
            }
            $lists2[$key] = $value;
        }
        //首页弹窗提示信息
        $msg_config = tpCache('index_msg');
        $this->assign('msg_config',$msg_config);

        $this->assign('type1',I('type'));
        $this->assign('case_list',$case_list);
        $this->assign('lists1',$lists1);
		$this->assign('lists2',$lists2);
		$this->assign('config',$config);
		$this->assign('navid','index');
        $this->display();
    }
	
	//使用协议
	 public function protocol(){
		 $msg = M('article')->where(array('article_id'=>53))->find();
		 $remess["msg"] = $msg["content"];
		 $json = json_encode($remess);
		 echo $json;
	 }
	 
	 //提交表单
	 public function seller_form(){
		$this->display();
	 }
	 
    /**
     * 客户买卖需求
     */

    public function sub_need(){
        $data=I();
        if($_SESSION['user']['user_id']){
            $data['user_id']=$_SESSION['user']['user_id'];
        }

        if(!$data['name'])
            $this->error('名称不能为空！',U('Home/Index/seller_form'));
        if(!$data['mobile'])
            $this->error('手机号不能为空！',U('Home/Index/seller_form'));
        if(!$data['content'])
            $this->error('需求内容不能为空！',U('Home/Index/seller_form'));

        $data['add_time']=time();
        $data['status']=0;
        $res=M('ask_buy')->add($data);
        if($res){
            $config = tpCache('shop_info');
            $to=$config['admin_email'];
            $title='新需求通知';
            $content=get_email_content_ask($res);
            new_send_email($to,$title,$content);
            $this->success('提交成功，客服人员将尽快与您联系。',U('Home/Index/index'));
        }else{
            $this->error('提交失败，请重试！',U('Home/Index/index'));
        }
    }

	public function seller_from(){
		$this->display();
	}




	public function set_kf_cookie(){
	    $kf_hide=I('kf_hide');
	    cookie('kf_hide',$kf_hide);
    }
    public function index_old(){
        /*$lang = L('_LANG_');
        $this->assign('wellcome', $lang);*/
        // 如果是手机跳转到 手机模块
        if(true == isMobile()){
            header("Location: ".U('Mobile/Index/index'));
        }
             $hot_goods = $hot_cate = $cateList = array();
             $sql = "select * from __PREFIX__goods as a left join ";
     //        $sql .= " __PREFIX__goods_category as b on a.cat_id=b.id where a.is_hot=1 and a.is_on_sale=1 order by a.sort";//二级分类下热卖商品
             $sql .= " __PREFIX__goods_category as b on a.cat_id=b.id where  1=1  ";//二级分类下热卖商品
     //        $index_hot_goods = M()->query($sql);//首页热卖商品
     //		if($index_hot_goods){
     //			foreach($index_hot_goods as $val){
     //				$cat_path = explode('_', $val['parent_id_path']);
     //				$hot_goods[$cat_path[1]][] = $val;
     //			}
     //		}
             $hot_category = M('goods_category')->where("is_hot=1 and level=3 and is_show=1")->select();//热门三级分类
             foreach ($hot_category as $v){
                 $cat_path = explode('_', $v['parent_id_path']);
                 $hot_cate[$cat_path[1]][] = $v;
             }

             foreach ($this->cateTrre as $k=>$v){
                 if($v['is_hot']==1){
                     $v['hot_goods'] = empty($hot_goods[$k]) ? '' : $hot_goods[$k];
                     $v['hot_cate'] = empty($hot_cate[$k]) ? '' : $hot_cate[$k];
                     $cateList[] = $v;
                 }
             }

        $this->assign('cateList',$cateList);
        $this->display();
    }


 
    /**
     *  公告详情页
     */
    public function notice(){
        $this->display();
    }
    
	public function ajaxHideTop()
    {       
       // $ids = I("ids"); // 商品 ids        
       // $result = M("Cart")->where(" id in ($ids)")->delete(); // 删除id为5的用户数据
	   if($_COOKIE["hidetopstatus"]=="show")
	   {
			//cookie('hidetopstatus','null',time() - 3600);
		   	//unset($_COOKIE['hidetopstatus']);
			//cookie('hidetopstatus','asdkfjsadlfadsf',time() - 3600);
		   	cookie('hidetopstatus','hide',3600);
			$return_arr = array('status'=>2,'msg'=>'','result'=>''); // 返回结果状态     
	   }
	   else
	   {
			cookie('hidetopstatus','show',3600);
			$return_arr = array('status'=>1,'msg'=>'','result'=>''); // 返回结果状态       
	   }
        exit(json_encode($return_arr));
    }
	
    // 二维码
    public function qr_code(){        
        // 导入Vendor类库包 Library/Vendor/Zend/Server.class.php
        //http://www.tp-shop.cn/Home/Index/erweima/data/www.99soubao.com
         require_once 'ThinkPHP/Library/Vendor/phpqrcode/phpqrcode.php';
          //import('Vendor.phpqrcode.phpqrcode');
            error_reporting(E_ERROR);            
            $url = urldecode($_GET["data"]);
            \QRcode::png($url);          
    }
    
    // 验证码
    public function verify()
    {
        //验证码类型
        $type = I('get.type') ? I('get.type') : '';
        $fontSize = I('get.fontSize') ? I('get.fontSize') : '40';
        $length = I('get.length') ? I('get.length') : '4';
        
        $config = array(
            'fontSize' => $fontSize,
            'length' => $length,
            'useCurve' => true,
            'useNoise' => false,
        );
        $Verify = new Verify($config);
        $Verify->entry($type);        
    }
    
    // 促销活动页面
    public function promoteList()
    {                          
        $Model = new \Think\Model();
        $goodsList = $Model->query("select * from __PREFIX__goods as g inner join __PREFIX__flash_sale as f on g.goods_id = f.goods_id   where ".time()." > start_time  and ".time()." < end_time");                        
        $brandList = M('brand')->getField("id,name,logo");
        $this->assign('brandList',$brandList);
        $this->assign('goodsList',$goodsList);
        $this->display();
    }
    
    function truncate_tables (){
        $model = new \Think\Model(); // 实例化一个model对象 没有对应任何数据表
        $tables = $model->query("show tables");
        $table = array('tp_admin','tp_config','tp_region','tp_system_module','tp_admin_role','tp_system_menu');
        foreach($tables as $key => $val)
        {                                    
            if(!in_array($val['tables_in_tpshop'], $table))                             
                echo "truncate table ".$val['tables_in_tpshop'].' ; ';
                echo "<br/>";         
        }                
    }

}